package com.sunlands.deskmate.message.handler.dispatcher;

import com.sunlands.deskmate.message.enums.MessageType;
import org.springframework.stereotype.Component;
import java.util.Map;

@Component
public class DispatcherManager {
    private volatile Map<String, Dispatcher> dispatcherMap;

    public DispatcherManager(Map<String, Dispatcher> dispatcherMap) {
        this.dispatcherMap = dispatcherMap;
    }

    public Dispatcher getDispatcher(String msgType) {
        //根据消息类似获取对应的消息接收器
        Class<Dispatcher> dispatcher = MessageType.of(msgType).getDispatcher();
        return dispatcherMap.values().stream()
                .filter(r -> r.getClass().getName().toLowerCase().equals(dispatcher.getName().toLowerCase()))
                .findFirst()
                .orElseThrow(NullPointerException::new);
    }
}
